package zhihui.mobile.ui.shop;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.zgm.utils.ui.CustomCheckTextView;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.address.ShoppingAddress;

/**
 * Created by 张公铭 on 2017/9/4 0004.
 */

public class ShopAddressDialogAdapter extends BaseAdapter
{
    private Context mContext;
    private LayoutInflater inflater;
    private List<ShoppingAddress> mList = new ArrayList<>();

    public ShopAddressDialogAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setAddressList(List<ShoppingAddress> list)
    {
        if (mList == null)
        {
            mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(list);
    }

    @Override
    public int getCount()
    {
        return mList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return mList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        CustomCheckTextView checkTextView;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.shop_address_dialog_item, null);
            checkTextView = (CustomCheckTextView) convertView.findViewById(R.id.cus_address_item);

            ViewCache viewCache = new ViewCache();
            viewCache.checkTextView = checkTextView;

            convertView.setTag(viewCache);
        } else
        {
            ViewCache viewCache = (ViewCache) convertView.getTag();
            checkTextView = viewCache.checkTextView;
        }
        ShoppingAddress address = mList.get(position);
        checkTextView.setChecked(false);
        if (address.getIsdefault() == 1)
        {
            checkTextView.setChecked(true);
        }
        checkTextView.setTitle(address.getAddress());
        return convertView;
    }

    class ViewCache
    {
        public CustomCheckTextView checkTextView;
    }
}
